package com.ly.demo.controller;

import cn.hutool.json.JSONUtil;
import com.ly.demo.entity.SmResult;
import com.ly.demo.service.WeixinUserService;
import com.ly.demo.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author https://www.wdbyte.com
 */
@Slf4j
@RestController
public class WeixinController {

    @Autowired
    private WeixinUserService weixinUserService;

    @Resource
    private RedisService redisService;

    @GetMapping(value = "/user/qrcode")
    public SmResult getQrCode() {
        return weixinUserService.getQrCode();
    }

    /**
     * 校验是否扫描完成
     * 完成，返回 JWT
     * 未完成，返回 check faild
     *
     * @param ticket
     * @return
     */
    @GetMapping(value = "/user/login/qrcode")
    public SmResult userLogin(String ticket) {
        String smResult = redisService.getQrCodeTicket(ticket);
        if (StringUtils.isNotEmpty(smResult)) {
            log.info("login success,open id:{}", smResult);
            return JSONUtil.toBean(smResult, SmResult.class);
        }
        log.info("login error,ticket:{}", ticket);
        return SmResult.ok();
    }

    @GetMapping(value = "/weixin/check")
    public String weixinCheck(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(timestamp) || StringUtils.isEmpty(nonce)) {
            return "";
        }
        weixinUserService.checkSignature(signature, timestamp, nonce);
        return echostr;
    }

    @PostMapping(value = "/weixin/check")
    public String weixinMsg(@RequestBody String requestBody, @RequestParam("signature") String signature,
                            @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce) {

        log.debug("requestBody:{}", requestBody);
        log.debug("signature:{}", signature);
        log.debug("timestamp:{}", timestamp);
        log.debug("nonce:{}", nonce);

        weixinUserService.checkSignature(signature, timestamp, nonce);
        return weixinUserService.handleWeixinMsg(requestBody);
    }

    @GetMapping(value = "/weixin/sendMsgByTemplate")
    public SmResult sendMsgByTemplate(@RequestParam String openId,@RequestParam String param) {

        return weixinUserService.sendMsgByTemplate(openId,param);
    }

    /**
     * h5-通过wx授权码登录
     *
     * @return
     */
    @GetMapping("/getTmpTokenByCode")
    public SmResult getTmpTokenByCode(@RequestParam String code,HttpServletRequest request, HttpServletResponse response) {
        return weixinUserService.getTmpTokenByCode(code,request,response);
    }
}
